package com.example.c201901090104;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.scwang.smart.refresh.footer.BallPulseFooter;
import com.scwang.smart.refresh.header.BezierRadarHeader;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;


public class TabFragment extends Fragment {
    int Page;
    RecyclerView recyclerView;
    List<News.ResultDTO.DataDTO> newslist = new ArrayList();
    MyAdapter myAdapter;
    SmartRefreshLayout smartRefreshLayout;
    public String gouzao(String type,int page){
        String a="http://v.juhe.cn/toutiao/index?type="+type+"&page="+page+"&page_size=10&is_filter=1&key=7225a4bbb66b70b038f45f767e7eefa4";
        return a;
    }


    public static TabFragment newInstance(String label) {
        Bundle args = new Bundle();
        args.putString("label", label);
        TabFragment fragment = new TabFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Page=1;
        return inflater.inflate(R.layout.fragment_tab, container, false);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        String label = getArguments().getString("label");//传进来的值
        recyclerView =getView().findViewById(R.id.recyclerView);
        smartRefreshLayout = getView().findViewById(R.id.smartRefreshLayout);
        newslist.clear();
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                x.Ext.init(getActivity().getApplication());//框架初始化
                RequestParams requestParams = new RequestParams(gouzao(label,Page));//请求地址构造
                x.http().get(requestParams, new Callback.CommonCallback<String>() {
                    @Override
                    public void onSuccess(String result) {
                        //Log.i("Main", result);
                        getActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Gson gson = new Gson();
                                News news = gson.fromJson(result, News.class);
                                for (int i = 0; i < news.result.data.size(); i++) {
                                    newslist.add(news.result.data.get(i));
                                }
                                myAdapter = new MyAdapter();
                                recyclerView.setAdapter(myAdapter);
                                recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

                            }
                        });
                    }

                    @Override
                    public void onError(Throwable ex, boolean isOnCallback) {
                        Toast.makeText(getActivity(), "error", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onCancelled(CancelledException cex) {

                    }

                    @Override
                    public void onFinished() {

                    }
                });
            }
        });
        thread.start();
        smartRefreshLayout.setRefreshFooter(new BallPulseFooter(getActivity()));
        smartRefreshLayout.setRefreshHeader(new BezierRadarHeader(getActivity()));
        smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                newslist.clear();
                Page=1;
                x.Ext.init(getActivity().getApplication());//框架初始化
                RequestParams requestParams = new RequestParams(gouzao(label,Page));//请求地址构造
                x.http().get(requestParams, new Callback.CommonCallback<String>() {
                    @Override
                    public void onSuccess(String result) {
                        Gson gson = new Gson();
                        News news = gson.fromJson(result, News.class);
                        for (int i = 0; i < news.result.data.size(); i++) {
                            newslist.add(news.result.data.get(i));
                        }
                        myAdapter.notifyDataSetChanged();
                    }
                    @Override
                    public void onError(Throwable ex, boolean isOnCallback) {
                        Toast.makeText(getActivity(), "error", Toast.LENGTH_SHORT).show();
                    }
                    @Override
                    public void onCancelled(CancelledException cex) {

                    }
                    @Override
                    public void onFinished() {

                    }
                });
                refreshLayout.finishRefresh(1000);
            }
        });
        smartRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                //newslist.clear();
                x.Ext.init(getActivity().getApplication());//框架初始化
                RequestParams requestParams = new RequestParams(gouzao(label,++Page));//请求地址构造
                x.http().get(requestParams, new Callback.CommonCallback<String>() {
                    @Override
                    public void onSuccess(String result) {
                        Gson gson = new Gson();
                        News news = gson.fromJson(result, News.class);
                        for (int i = 0; i < news.result.data.size(); i++) {
                            newslist.add(news.result.data.get(i));
                        }
                        myAdapter.notifyDataSetChanged();
                    }
                    @Override
                    public void onError(Throwable ex, boolean isOnCallback) {
                        Toast.makeText(getActivity(), "error", Toast.LENGTH_SHORT).show();
                    }
                    @Override
                    public void onCancelled(CancelledException cex) {

                    }
                    @Override
                    public void onFinished() {

                    }
                });
                refreshLayout.finishLoadMore(1000);
            }
        });

    }

    public class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {

        @NonNull
        @Override
        public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(getActivity()).inflate(R.layout.list1_item, parent, false);
            MyViewHolder myViewHolder = new MyViewHolder(view);
            return myViewHolder;
        }

        @Override
        public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
            holder.nameTv.setText(newslist.get(position).title);
            holder.contentTv.setText(newslist.get(position).author_name);
            holder.timeTv.setText(newslist.get(position).date);
            Glide.with(getActivity())
                    .load(newslist.get(position).thumbnail_pic_s).transform(new GlideRectRound(getActivity(), 5))
                    .into(holder.imageView);
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(getActivity(), Click_news.class);
                    intent.putExtra("url", newslist.get(position).url);
                    startActivity(intent);
                    //Toast.makeText(getActivity(),"点击",Toast.LENGTH_SHORT).show();
                }
            });
        }

        @Override
        public int getItemCount() {
            return newslist.size();
        }


    }


    public class MyViewHolder extends RecyclerView.ViewHolder {
        TextView nameTv;
        TextView contentTv;
        TextView timeTv;
        ImageView imageView;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            nameTv = itemView.findViewById(R.id.titleView);
            contentTv = itemView.findViewById(R.id.contentView);
            timeTv = itemView.findViewById(R.id.timeView);
            imageView = itemView.findViewById(R.id.imageView);
        }
    }
}